﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//new
using System.IO;
using System.Xml;

namespace JKZP
{
    public partial class WorkTongJiForm : Form
    {
        private string[] username_t = null;
        private int[] total_t = null;
        private int[] pass_t = null;
        private int[] fail_t = null;
        private int[] left_t = null;
        private bool success = false;

        public WorkTongJiForm()
        {
            InitializeComponent();
        }

        private void WorkTongJiForm_Load(object sender, EventArgs e)
        {
            try
            {
                dateTimePicker1.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
                dateTimePicker2.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);

                string[] bms = File.ReadAllLines(PP.gSSBMFile);

                if ((bms != null) && (bms.Length > 0))
                {
                    DataTable dt = new DataTable();
                    dt.Columns.Add("bmcode");
                    dt.Columns.Add("bmname");
                    dt.Rows.Clear();
                    foreach (string s in bms)
                    {
                        string[] ss = s.Split('-');
                        if ((ss != null) && (ss.Length > 0))
                        {
                            dt.Rows.Add(new string[2] { ss[0], ss[1] });
                        }
                    }
                    comboBox1.Items.Clear();
                    comboBox1.DisplayMember = "bmname";
                    comboBox1.ValueMember = "bmcode";
                    comboBox1.DataSource = dt;
                    comboBox1.SelectedIndex = 0;
                }
            }
            catch { }
        }

        private void button_tongji_Click(object sender, EventArgs e)
        {
            try
            {
                
                string error = "";
                this.dataGridView1.Rows.Clear();

                bool flag = PP.kdws.JKZP_TongJi(comboBox1.SelectedValue.ToString(),
                    dateTimePicker1.Value.ToString("yyyy-MM-dd HH:mm:ss"),
                    dateTimePicker2.Value.ToString("yyyy-MM-dd HH:mm:ss"),
                    out username_t, out total_t, out pass_t, out fail_t, out left_t, out error);
                if(flag==true)
                {
                    if ((username_t != null) && (total_t != null) && (pass_t != null) && (fail_t != null) && (left_t != null))
                    {
                        for (int i = 0; i < username_t.Length; i++)
                        {
                            dataGridView1.Rows.Add(new string[5] { username_t[i], total_t[i].ToString(), pass_t[i].ToString(), fail_t[i].ToString(), left_t[i].ToString() });
                        }
                        this.toolStripStatusLabel1.Text = "统计成功";
                        success = true;
                    }
                    else
                    {
                        success = false;
                        this.toolStripStatusLabel1.Text = "统计参数为空";
                    }
                }
                else
                {
                    success = false;
                    if (error == "")
                    {
                        this.toolStripStatusLabel1.Text = "共0条记录";
                    }
                    else
                    {
                        this.toolStripStatusLabel1.Text = "统计失败" + error;
                    }
                    
                }
            }
            catch (Exception er)
            {
                success = false;
                this.toolStripStatusLabel1.Text = "统计异常" + er.Message;
            }
        }

        private void button_daochu_Click(object sender, EventArgs e)
        {
            try
            {
                if (success == true)
                {
                    this.saveFileDialog1.FileName = comboBox1.Text  + dateTimePicker1.Text + "-" + dateTimePicker2.Text+ "工作量统计报表";
                        
                    if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        string[] lines = new string[username_t.Length+1];
                        lines[0] = "抓拍员  总数    有效    无效    待审";
                        for (int i=0;i<username_t.Length;i++)
                        {
                            lines[i+1] = username_t[i] + "\t" + total_t[i].ToString() + "\t" + pass_t[i].ToString() + "\t" + fail_t[i].ToString() + "\t" + left_t[i].ToString();
                        }
                        File.WriteAllLines(this.saveFileDialog1.FileName, lines);
                        this.toolStripStatusLabel1.Text = "导出成功";
                    }
                }
            }
            catch { }
        }

    }
}
